package Domd5;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.util.Arrays;

public class test {
    public static void main(String[] args) {
        Student[] student=new Student[2];
        student[0]=new  Student("Lisa",18);
        student[1]=new  Student("Lili",16);
        System.out.println("排序前" + Arrays.toString(student));
        Student.mySort(student);
        System.out.println("排序后" + Arrays.toString(student));
    }
}
class Student implements Comparable <Student>{//用到Comparable接口
    public String name;
    public int age;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public  static void mySort(Comparable[] comparables){
        for (int i = 0; i < comparables.length-1; i++) {
            for (int j = 0; j < comparables.length-1-i; j++) {
                if(comparables[j].compareTo(comparables[j+1])>0){
                    Comparable tmp=comparables[j];
                    comparables[j]=comparables[j+1];
                    comparables[j+1]=tmp;
                }
            }

        }
    }

   @Override
    public int compareTo(Student o) {
       if(this.age>o.age)
        {
            return 1;
        }else if(this.age>o.age){
            return 0;
       }else {
           return -1;
       }
        //return this.name.compareTo(o.name);
   }
}
